/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.model.rdb;

/**
 * <p>Title: KnowldgeBase to Database</p>
 * <p>Description: Transform Protege Knowledgebase RDB schema</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: University of Washington BHI</p>
 * @author Hao Li
 * @version 2.0
 */
import seedpod.util.XMLWriter;

public class SimpleForeignKey extends Attribute
{

    private Attribute _reference;

    public SimpleForeignKey(Rdb schema, Relation fromTable, String fromAtt,
            Relation toTable, String toAtt, RdbValueType type) throws NoRelationFoundException {
        super( schema, fromTable, fromAtt, type );
        setReferencedAttribute( toTable );
    }

    public Attribute getReferencedAttribute() {
        return _reference;
    }

    public void setReferencedAttribute( Relation table ) {
        Attribute toPK = table.getPrimaryKey();
        _reference = toPK;
    }

    public XMLWriter serializeXML( XMLWriter out )
            throws XMLWriter.XMLException {
        XMLWriter fk;
        fk = out.addElement( "foreignKey" );
        fk.addElement( "fkName", getName() );
        fk.addElement( "referenceTable", getContainerRelationName() );
        fk
                .addElement( "referencedTable", _reference
                        .getContainerRelationName() );
        fk.addElement( "referencedColumn", _reference.getName() );
        return out;
    }

}